<template>
    <el-row class="tac">
            <el-col :span="4">
                <el-menu
                    :default-active="defaultActive"
                    class="el-menu-vertical-demo"
                    @open="handleOpen"
                    @close="handleClose"
                    @select="select"
                    unique-opened
                    background-color="#545c64"
                    text-color="#fff"
                    active-text-color="#ffd04b">
                        <router-link to="/"><el-menu-item><i class="el-icon-s-home"></i>首页</el-menu-item></router-link>
                        <router-link to="/chart"><el-menu-item><i class="el-icon-s-data"></i>图表</el-menu-item></router-link>
                        <router-link to="/shezhi"><el-menu-item><i class="el-icon-s-tools"></i>设置</el-menu-item></router-link>

                        <!-- 带有二级导航的 -->
                        <div v-if="haveChildren">
                            <el-submenu v-for="item in haveChildren" :key="item.id" :index="String(item.id)">
                                <template slot="title">
                                <i :class="item.meta.icon"></i>
                                <span>{{item.title}}</span>
                                </template>
                                <router-link v-for="value in item.children" :key="value.id" :to="value.link"><el-menu-item :index="String(item.id)">{{value.title}}</el-menu-item></router-link>
                            </el-submenu>
                        </div>

                        <!-- 一级导航 -->
                        <div v-if="noHaveChildren">
                            <router-link v-for="value in noHaveChildren" :key="value.id" :to="value.path"><el-menu-item><i :class="value.meta.icon"></i>{{value.title}}</el-menu-item></router-link>
                        </div>
                </el-menu>
            </el-col>
        </el-row>
</template>

<script>
import { mapState } from 'vuex'
export default {
    data(){
        return {
           haveChildren: [],
           noHaveChildren: [],
           defaultActive: sessionStorage.getItem('check_Key') ? sessionStorage.getItem('check_Key') : ''
        }
    },
    computed:{
        // 拿出仓库中的数据
        ...mapState(['userPower'])
    },
    created(){
        // 把有children的和没有的查分开，因为便利的时候要知道他有没有二级导航
        this.userPower.forEach(item => {
            if(item.children) this.haveChildren.push(item)
            else this.noHaveChildren.push(item)
        })
    },
    methods: {
      handleOpen(key, keyPath) {
        // console.log(key, keyPath);
      },
      handleClose(key, keyPath) {
        // console.log(key, keyPath);
      },
      select(k, p){
          this.defaultActive = k
          sessionStorage.setItem('check_Key', k)
      },
    },
}
</script>

<style scoped>
    .el-row{
        height: calc(100% - 80px);
        width: 200px;
        margin: 20px 20px 0 0;
        box-sizing: border-box;
        box-shadow:0px 0px 5px 5px #20AAE9;
    }
    .el-col{
        height: 100%;
        width: 200px;
    }
    .el-menu{
        height: 100%;
        background-color: rgba(0,0,0,.5) !important;
    }
    .el-menu li ,ul{
        background-color: rgba(0,0,0,0) !important;
        color: #fff !important;
    }
    >>>.el-menu li>ul{
        background-color: rgba(0,0,0,.2) !important;
    }
    .el-menu div{
        background-color: rgba(0,0,0,0) !important;

    }
    .el-menu .el-menu-item{
        background-color: rgba(0,0,0,0) !important;
    }
    .el-menu .el-menu-item:hover{
        background-color: rgb(22, 22, 22) !important;
        box-shadow:0px 0px 2px 2px #20AAE9;
    }
</style>